package com.rbs.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rbs.entity.Tea;
import com.rbs.mapper.TeaMapper;
import com.rbs.service.TeaService;
import com.rbs.util.MybatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

public class TeaServiceImpl implements TeaService {
    @Override
    public List<Tea> selectAllTeaAndCategory() {
        SqlSession session = null;
        List<Tea> teas = null;
        try {
            session = MybatisUtils.getSession();
            TeaMapper mapper = session.getMapper(TeaMapper.class);
            teas = mapper.selectAllTeaAndCategory();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MybatisUtils.colesSession(session);
        }
        return teas;
    }

    @Override
    public Tea selectTeaAndCategoryById(int id) {
        SqlSession session = null;
        Tea tea = null;
        try {
            session = MybatisUtils.getSession();
            TeaMapper mapper = session.getMapper(TeaMapper.class);
            tea = mapper.selectTeaAndCategoryById(id);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MybatisUtils.colesSession(session);
        }
        return tea;
    }

    @Override
    public void deleteTeaById(int id) {
        SqlSession session = null;
        try {
            session = MybatisUtils.getSession();
            TeaMapper mapper = session.getMapper(TeaMapper.class);
            mapper.deleteTeaById(id);
            session.commit();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MybatisUtils.colesSession(session);
        }
    }

    @Override
    public void updateTea(Tea tea) {
        SqlSession session = null;
        try {
            session = MybatisUtils.getSession();
            TeaMapper mapper = session.getMapper(TeaMapper.class);
           mapper.updateTea(tea);
            session.commit();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MybatisUtils.colesSession(session);
        }
    }

    @Override
    public void insertTeaAndCategory(Tea tea) {
        SqlSession session = null;
        try {
            session = MybatisUtils.getSession();
            TeaMapper mapper = session.getMapper(TeaMapper.class);
           mapper.insertTeaAndCategory(tea);
            session.commit();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MybatisUtils.colesSession(session);
        }
    }

    @Override
    public void deleteTeaByCateId(int cateId) {
        SqlSession session = null;
        try {
            session = MybatisUtils.getSession();
            TeaMapper mapper = session.getMapper(TeaMapper.class);
            mapper.deleteTeaByCateId(cateId);
            session.commit();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MybatisUtils.colesSession(session);
        }
    }

    @Override
    public List<Tea> selectAllTeaAndCategoryByCategory(String categoryName) {
            SqlSession session = null;
            List<Tea> teas = null;
            try {
                session = MybatisUtils.getSession();
                TeaMapper mapper = session.getMapper(TeaMapper.class);
                teas = mapper.selectAllTeaAndCategoryByCategory(categoryName);
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                MybatisUtils.colesSession(session);
            }
        return teas;
    }

    @Override
    public PageInfo<Tea> selectTeaByPage(int curPage, int PageSize) {
        SqlSession session = null;
        PageInfo<Tea> teaPageInfo = null;
        try {
            session = MybatisUtils.getSession();
            TeaMapper mapper = session.getMapper(TeaMapper.class);
            //第一个参数是页数，第二个参数是一页展示几条数据
            PageHelper.startPage(curPage,PageSize);
            List<Tea> teas = mapper.selectAllTeaAndCategory();
            teaPageInfo = new PageInfo<>(teas);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MybatisUtils.colesSession(session);
        }
        return teaPageInfo;
    }

    @Override
    public void deleteMany(int[] ids) {
        SqlSession session = null;
        try {
            session = MybatisUtils.getSession();
            TeaMapper mapper = session.getMapper(TeaMapper.class);
            mapper.deleteMany(ids);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MybatisUtils.colesSession(session);
        }
    }

    @Override
    public List<Tea> selectByPage(String name, BigDecimal price) {
        SqlSession session = null;
        List<Tea> teas = null;
        try {
            session = MybatisUtils.getSession();
            TeaMapper mapper = session.getMapper(TeaMapper.class);
            teas = mapper.selectByPage(name, price);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MybatisUtils.colesSession(session);
        }
        return teas;
    }
}
